// PIC18F14K50 distance sensor
// FileName: distance sensor



#include <p18f14k50.h>
#include <delays.h>
#include <timers.h>

#pragma config CPUDIV=NOCLKDIV //CPUデバイダ÷1
#pragma config USBDIV = OFF //USBデバイダ÷1
#pragma config FOSC = HS //外部クロック使用
#pragma config PLLEN = ON //PLL×4
#pragma config PCLKEN = OFF
#pragma config FCMEN = OFF
#pragma config IESO = OFF
#pragma config PWRTEN = OFF,BOREN = OFF
#pragma config WDTEN = OFF
#pragma config MCLRE = OFF
#pragma config HFOFST = OFF,STVREN = OFF,LVP = OFF,BBSIZ = OFF,XINST = OFF
#pragma config CP0 = OFF,CP1 = OFF,CPB = OFF,CPD = OFF
#pragma config WRT0 = OFF,WRT1 = OFF,WRTB = OFF,WRTC = OFF,WRTD = OFF
#pragma config EBTR0 = OFF,EBTR1 = OFF,EBTRB = OFF
#pragma config BORV = 30
#pragma config WDTPS = 32768

#define lcd_DC LATC
#define lcd_E LATBbits.LATB4
#define lcd_RS LATBbits.LATB6

#define Delay_50uS Delay100TCYx(6)
#define Delay_1mS Delay1KTCYx(12)
#define Delay_2mS Delay1KTCYx(24)
#define Delay_5mS Delay10KTCYx(6)
#define Delay_10mS Delay10KTCYx(12)
#define Delay_20mS Delay10KTCYx(24)
#define Delay_100mS Delay10KTCYx(120)


unsigned char i, ss;
unsigned int j, n, N;
double T;

void lcd_w4(char asci);
void lcd_c4(char cmd);
void lcd_clr(void);
void lcd_init(void);
void lcd_puts(char *s);
void lcd_locate(char y,char x);

void lcd_putN(unsigned int N);

void UserInit(void);
void lcd_Init(void);

void high(void);
void low(void);


#pragma interrupt high
#pragma interruptlow low
#pragma code H_vect = 0x0008
void H_isr(void){
_asm goto high _endasm
}
#pragma code L_vect = 0x0018
void L_isr(void){
_asm goto low _endasm
}
#pragma code



void high(){

if(INTCONbits.INT0IF){
if(ss == 0){
ss = 1;
} else {
ss = 0;
}
Delay_100mS;Delay_100mS;

INTCONbits.INT0IF = 0; // 割り込みフラグを0に戻す
}
}


void low(){
}



void lcd_w4(char c){
lcd_DC = (c & 0xF0) | (lcd_DC & 0x0F);
lcd_E = 1;
Delay1TCY();
lcd_E = 0;
}

void lcd_c4(char c){
lcd_RS = 0;
lcd_w4(c);
lcd_w4(c<<4);
if(c & 0xFC)
Delay_50uS;
else
Delay_2mS;
}

void lcd_clr(void){
lcd_c4(0x01);
}

void lcd_locate(char y, char x){
unsigned char p;
switch(y & 0x03){
case 0: p=0x80; break;
case 1: p=0xC0; break;
case 2: p=0x94; break;
case 3: p=0xD4; break;
}
lcd_c4(p += x);
}

void lcd_putchr(char c){
lcd_RS = 1;
lcd_w4(c);
lcd_w4(c<<4);
Delay_50uS;
}

void lcd_puts(char *s){
while(*s)
lcd_putchr(*s++);
}


void lcd_putN(unsigned int N){
unsigned char buf[4];

for(i=0; i<4; i++) buf[i] = ' ';

i=3;
do{
buf[i]=(N%10)+'0';
N=N/10;
i--;
} while(N>0);
for(i=0; i<4; i++) lcd_putchr(buf[i]);
}



void lcd_init(void){
Delay_20mS;
lcd_RS = 0;
lcd_w4(0x30);Delay_5mS;
lcd_w4(0x30);Delay_1mS;
lcd_w4(0x30);Delay_1mS;
lcd_w4(0x20);Delay_1mS;
lcd_c4(0x2E);
lcd_c4(0x08);
lcd_c4(0x0C);
lcd_c4(0x06);
lcd_c4(0x01);
}

void UserInit(void){
N = 0; ss = 0;
}

char Distance[] = "Distance";
char D[] = "D = ";
char mm[] = "(mm)";
char Error[] = "Error";
char blank[] = " ";
char STOP[] = " STOP";



void main(void){

PORTA = 0x00;
PORTB = 0x00;
PORTC = 0x00;
TRISA = 0b00000000;
TRISB = 0b00000000;
TRISC = 0b00000101; // RC0入力用(INT0)、RC2入力用
ANSEL = 0b00000000;
ANSELH = 0b00000000;

INTCONbits.INT0IE = 1;// INT0 割り込み  1で有効
INTCONbits.INT0IF = 0;// フラグ

RCONbits.IPEN = 1;// 高優先・低優先  2段階
INTCONbits.GIEH = 1;// 高優先割込み  許可
INTCONbits.GIEL = 1;// 低優先割込み  許可

lcd_init();
UserInit();



while(1){

if(ss == 0){

PORTBbits.RB7 = 1; // スタート・トリガパルスを送る
Delay10TCYx(12); // 10μS
PORTBbits.RB7 = 0;


n = 0;
for(j=0; j<10000; j++){ // サンプリング
if(PORTCbits.RC2) n++;
else{
}
Delay10TCYx(12); // 10μS = 0.01mS

}

T = n; // unsigned int n を doubleT に型変換
T = T * 2.010; // 補正を含めた計算
N = T; // doubleT を unsigned int N に型変換


lcd_clr();
lcd_locate(0,0);
lcd_puts(blank);lcd_puts(blank);
lcd_puts(Distance);

lcd_locate(1,0);
lcd_puts(blank);
lcd_puts(D);lcd_puts(blank);

if((N>30)&&(N<4500)){
lcd_putN(N);
lcd_puts(blank);
lcd_puts(mm);
} else {
lcd_puts(Error);
}


} else {

Delay_100mS;

lcd_clr();
lcd_locate(0,0);
lcd_puts(blank);lcd_puts(blank);
lcd_puts(Distance);
lcd_puts(STOP);

lcd_locate(1,0);
lcd_puts(blank);
lcd_puts(D);lcd_puts(blank);

if((N>30)&&(N<4500)){
lcd_putN(N);
lcd_puts(blank);
lcd_puts(mm);
} else {
lcd_puts(Error);
}

}


Delay_100mS;
}

}



               戻る